package cn.ch.pet.controller;

import cn.ch.basic.exception.BusinessException;
import cn.ch.basic.util.AjaxResult;
import cn.ch.basic.util.LoginContext;
import cn.ch.org.domain.Employee;
import cn.ch.pet.domain.Product;
import cn.ch.pet.query.ProductQuery;
import cn.ch.pet.service.IProductService;
import cn.ch.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    public IProductService productService;


    /**
     * 保存和修改公用的
     * @param product  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Product product, HttpServletRequest request){
        try {
            //1.获取到的登录信息
            Object login = LoginContext.getLogin(request);
            //2.转换成用户信息（可以强制转换）
            Employee employee = (Employee) login;
            //2.判断获取到的id来进行添加和修改
            if( product.getId()!=null) {
                //3.修改宠物信息
                productService.update(product);
            }else {
                //4.添加用户信息
                productService.addProductDetail(product,employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            productService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Product get(@PathVariable("id")Long id)
    {
        return productService.queryById(id);
    }



    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Product> list(){

        return productService.queryAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Product> json(@RequestBody ProductQuery query)
    {
        return productService.queryList(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batch(@RequestBody List<Long> ids){
        try {
            productService.batch(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统报错了，我们正在殴打程序员！！");
        }
    }

    /**
     * 实现宠物上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody List<Long> ids) {
        try {
            //批量上架
            productService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }
    }

    /**
     * 宠物下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody List<Long> ids){
        try {
            //批量下架
            productService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！");
        }

    }


}
